package uiLayer;
import controlLayer.*;
import modelLayer.*;
import java.util.Scanner;

public class EmployeeUI {
   private EmployeeCtr employeeCtr;
    
   public EmployeeUI() {
       employeeCtr = new EmployeeCtr();
    }
    
   /**
     * Employees menu methods
     */
    public void employeesMenu() {
        boolean stop = false;
        
        while(!stop) {
            int choice = writeEmployeesMenu();
            
            switch(choice) {
            case 1 : createEmployee();
                break;
            case 2 : findEmployee();
                break;
            case 3 : updateEmployee();
                break;
            case 4 : removeEmployee();
                break;
            case 0 : stop = true;
                break;
            default : System.out.println("Invalid command");
                      MainMenuUI.pause();
                break;
            }
        }
    } 
    
    private int writeEmployeesMenu() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f*** Employees Menu ***");
        System.out.println("1. Add employee ");
        System.out.println("2. Find employee ");
        System.out.println("3. Update employee ");
        System.out.println("4. Remove employee ");
        System.out.println("0. Return to main menu ");
        System.out.println("\n Type your choice");
        
        int choice = keyboard.nextInt();
        
        return choice;
    }
    
    /**
     * Employee information input
     */
    private String inputName() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the name : ");
        String name = keyboard.nextLine();
        if(!name.isEmpty()) {
            return name;
        }
        else {
            return inputName();
        }
    }
    
    private String inputPhone() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the phone number of the person : ");
        String phone = keyboard.nextLine();
         if(!phone.isEmpty()) {
            return phone;
        }
        else {
            return inputPhone();
        }
    }
     
    private String inputEmail() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the email : ");
        String email = keyboard.nextLine();
        return email;
    }
    
    private String inputAddress() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the address : ");
        String address = keyboard.nextLine();
        return address;
    }
   
    public int inputCprNo() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the CPR number of the employee : ");
        int cprNo = keyboard.nextInt();
        return cprNo;
    }
    
    public String inputPassword() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the password of the employee : ");
        String password = keyboard.nextLine();
         if(!password.isEmpty()) {
            return password;
        }
        else {
            System.out.println("The password cannot be empty");
            return inputPassword();
        }
    }
    
    public String inputDepartment() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the department of the employee : ");
        String department = keyboard.nextLine();
         if(!department.isEmpty()) {
            return department;
        }
        else {
            System.out.println("Please use one of the following options : \"timber\" or \"center\" ");
            return inputName();
        }
    }
    
    public String inputType() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the type of the employee : ");
        String type = keyboard.nextLine();
         if(!type.isEmpty()) {
            return type;
        }
        else {
            System.out.println("Please use 1 for Managers or 2 for regular employees ");
            return inputType();
        }
    }
    
    /**
     * Employee information show
     */
    private void showName(String name){
        System.out.println("Name : " + name);
    }
    
    private void showPhone(String phone){ 
        System.out.println("Phone : " + phone);
    }
    
    private void showEmail(String email){ 
        System.out.println("Email : " + email);
    }

    private void showAddress(String address) { 
        System.out.println("Address : " + address);
    }
    
    private void showPassword(String password){ 
        System.out.println("Password : " + "**********");
    }
    
    private void showDepartment(String department){ 
        System.out.println("Department : " + department);
    }

    private void showType(String type) { 
        System.out.println("Type : " + type);
    }
    
    /**
     * find Employee
     */
    private void findEmployee() {
        int cprNo = inputCprNo();
        Employee emp = (employeeCtr.findEmployee(cprNo));
        if(emp != null) {
            System.out.println();
            showName(emp.getName());
            showPhone(emp.getPhone());
            showEmail(emp.getEmail());
            showAddress(emp.getAddress());
            showPassword(emp.getPassword());
            showDepartment(emp.getDepartment());
            showType(emp.getType());
            
        }
        else { 
            System.out.println("No employee was found with the specified CPR number.");
        }
        System.out.println();
        MainMenuUI.pause();
        
    }
    
    /**
     * Update employee
     */
    private void updateEmployee() {
        Scanner keyboard = new Scanner(System.in);
        int cprNo = inputCprNo();
        Employee emp = (employeeCtr.findEmployee(cprNo));           
        boolean stop = false;
        if (emp != null) {
            System.out.println();
            showName(emp.getName());
            showPhone(emp.getPhone());
            showEmail(emp.getEmail());
            showAddress(emp.getAddress());
            showDepartment(emp.getDepartment());
            showType(emp.getType());
            System.out.println();
            System.out.println("1. Update name");
            System.out.println("2. Update phone");
            System.out.println("3. Update email");
            System.out.println("4. Update address");
            System.out.println("6. Update department");
            System.out.println("7. Update type");
            System.out.println("0. Return to previous menu");
            System.out.println();
            System.out.println("Enter your choice : ");
            
            int choice = keyboard.nextInt();
            
            while(!stop) {
                stop = true;
                
                switch(choice) {
                case 1 :emp.setName(inputName());
                    break;
                case 2 :emp.setPhone(inputPhone());
                    break;
                case 3 :emp.setEmail(inputEmail());
                    break;
                case 4 :emp.setAddress(inputAddress());
                    break;
                case 5 :emp.setDepartment(inputDepartment());
                    break;
                case 6 :emp.setType(inputType());
                    break;
                case 0 : stop= true;
                }
                System.out.println();
                System.out.println("The information was updated");
                System.out.println();
            }
            
        }
        else {
            System.out.println("No employee was found with the specified CPR number.");
        }
        MainMenuUI.pause();
    }
    
    /**
     * removeempomer
     */
    private void removeEmployee() {
        Scanner keyboard = new Scanner(System.in);
        int cprNo = inputCprNo();
        Employee emp = (employeeCtr.findEmployee(cprNo));
        boolean stop = false;
        if (emp != null) {
            System.out.println();
            showName(emp.getName());
            showAddress(emp.getPhone());
            showEmail(emp.getEmail());
            showAddress(emp.getAddress());
            showDepartment(emp.getDepartment());
            showType(emp.getType());
            
            System.out.println();
            System.out.println("1.Remove employee");
            System.out.println("0.Return to previous menu");
            System.out.println();
            System.out.println("Enter your choice (numeric) :");
            int choice = keyboard.nextInt();
            
            while(!stop) {
                stop = true;
                
                switch(choice) {
                case 1 : employeeCtr.removeEmployee(cprNo);
                        System.out.println();
                        System.out.println("The employee was successfully removed");
                        System.out.println();
                        MainMenuUI.pause();
                    break;
                case 0 : stop = true;
                    break;
                }
            }
        }
        else {
            System.out.println("No employee was found with the specified CPR number.");
        }
    }
    
    /**
     * Friends crerate person
     */
    private void createEmployee() {
        String name = inputName();
        String phone = inputPhone();
        String email = inputEmail();
        String address = inputAddress();
        int cprNo = inputCprNo();
        String password = inputPassword();
        String department = inputDepartment();
        String type = inputType();
        
        employeeCtr.createEmployee(name, phone, email, address, cprNo, password, department, type);
        System.out.println();
        System.out.println("The employee was successfully created");
        System.out.println();
        MainMenuUI.pause();
    }
}